package view;

import javax.swing.*;
import java.awt.*;
import model.User;

public class MainFrame extends JFrame {
    private JMenuBar menuBar;
    private User currentUser;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    public MainFrame() {
        // 显示登录对话框
        LoginDialog loginDialog = new LoginDialog(this);
        loginDialog.setVisible(true);
        
        currentUser = loginDialog.getLoggedInUser();
        if (currentUser == null) {
            System.exit(0);
        }
        
        initUI();
    }
    
    private void initUI() {
        setTitle("图书销售管理系统 - " + currentUser.getUsername());
        setSize(1024, 768);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // 初始化菜单栏
        initMenuBar();
        
        // 初始化主面板
        initMainPanel();
        
        setLocationRelativeTo(null);
    }
    
    private void initMenuBar() {
        menuBar = new JMenuBar();
        
        // 文件菜单
        JMenu fileMenu = new JMenu("文件");
        fileMenu.setMnemonic('F');
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.setAccelerator(KeyStroke.getKeyStroke("ctrl X"));
        exitItem.addActionListener(e -> System.exit(0));
        fileMenu.add(exitItem);
        
        // 图书管理菜单
        JMenu bookMenu = new JMenu("图书管理");
        bookMenu.setMnemonic('B');
        JMenuItem addBookItem = new JMenuItem("添加图书");
        JMenuItem searchBookItem = new JMenuItem("查询图书");
        bookMenu.add(addBookItem);
        bookMenu.add(searchBookItem);
        
        // 销售管理菜单
        JMenu salesMenu = new JMenu("销售管理");
        salesMenu.setMnemonic('S');
        JMenuItem newSaleItem = new JMenuItem("新建销售");
        salesMenu.add(newSaleItem);
        
        // 添加新建销售的事件监听
        newSaleItem.addActionListener(e -> {
            SellDialog dialog = new SellDialog(this, (SalesPanel)((JTabbedPane)getContentPane().getComponent(0)).getSelectedComponent());
            dialog.setVisible(true);
            // 切换到销售管理标签页
            ((JTabbedPane)getContentPane().getComponent(0)).setSelectedIndex(1);
        });
        
        // 统计报表菜单
        JMenu statsMenu = new JMenu("统计报表");
        statsMenu.setMnemonic('R');
        JMenuItem salesStatsItem = new JMenuItem("销售统计");
        statsMenu.add(salesStatsItem);
        
        // 用户管理菜单
        JMenu userMenu = new JMenu("用户管理");
        userMenu.setMnemonic('U');
        JMenuItem userManageItem = new JMenuItem("用户管理");
        userMenu.add(userManageItem);
        
        // 添加用户管理菜单的事件监听
        userManageItem.addActionListener(e -> {
            // 创建用户管理窗口
            JFrame userFrame = new JFrame("用户管理");
            userFrame.setSize(800, 600);
            userFrame.setLocationRelativeTo(this);
            userFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            userFrame.add(new UserPanel());
            userFrame.setVisible(true);
        });
        
        // 帮助菜单
        JMenu helpMenu = new JMenu("帮助");
        helpMenu.setMnemonic('H');
        JMenuItem aboutItem = new JMenuItem("关于");
        helpMenu.add(aboutItem);
        
        menuBar.add(fileMenu);
        menuBar.add(bookMenu);
        menuBar.add(salesMenu);
        menuBar.add(statsMenu);
        menuBar.add(userMenu);
        menuBar.add(helpMenu);
        
        // 添加事件监听
        addBookItem.addActionListener(e -> {
            // 获取当前的BookPanel实例
            JTabbedPane tabbedPane = (JTabbedPane)getContentPane().getComponent(0);
            BookPanel bookPanel = (BookPanel)tabbedPane.getComponentAt(0);
            
            // 切换到图书管理标签页
            tabbedPane.setSelectedIndex(0);
            
            // 创建并显示添加图书对话框
            AddBookDialog dialog = new AddBookDialog(this, bookPanel);
            dialog.setVisible(true);
        });
        
        searchBookItem.addActionListener(e -> {
            // 切换到图书管理标签页
            JTabbedPane tabbedPane = (JTabbedPane)getContentPane().getComponent(0);
            tabbedPane.setSelectedIndex(0);
            
            // 获取BookPanel实例并请求焦点到搜索框
            BookPanel bookPanel = (BookPanel)tabbedPane.getComponentAt(0);
            bookPanel.requestSearchFocus();
        });
        
        salesStatsItem.addActionListener(e -> {
            StatsFrame statsFrame = new StatsFrame();
            statsFrame.setVisible(true);
        });
        
        aboutItem.addActionListener(e -> {
            JOptionPane.showMessageDialog(this,
                "图书销售管理系统 v1.0\n作者：和江峰\n版权所有 © 2024",
                "关于",
                JOptionPane.INFORMATION_MESSAGE);
        });
        
        // 根据用户角色设置权限
        if (!"admin".equals(currentUser.getRole())) {
            addBookItem.setEnabled(false);
            statsMenu.setEnabled(false);
            userMenu.setEnabled(false);
        }
        
        setJMenuBar(menuBar);
    }
    
    private void initMainPanel() {
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        
        // 添加标签页
        tabbedPane.addTab("图书管理", new BookPanel());
        tabbedPane.addTab("销售管理", new SalesPanel());
        
        // 设置标签页提示
        tabbedPane.setToolTipTextAt(0, "管理图书信息");
        tabbedPane.setToolTipTextAt(1, "管理销售记录");
        
        add(tabbedPane, BorderLayout.CENTER);
        
        // 添加状态栏
        JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
        statusBar.setBorder(BorderFactory.createEtchedBorder());
        JLabel statusLabel = new JLabel("当前用户：" + currentUser.getUsername() + " | 角色：" + currentUser.getRole());
        statusBar.add(statusLabel);
        
        add(statusBar, BorderLayout.SOUTH);
    }
} 